package com.wry.es.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fandow.common.core.pagination.PageInfo;
import com.fandow.common.core.pagination.Paging;
import com.fandow.common.core.utils.PageUtils;
import com.wry.es.dao.OmsOrder;
import com.wry.es.dto.OrderPageDTO;
import com.wry.es.mapper.OmsOrderMapper;
import com.wry.es.repository.ElasticOrderRepository;
import com.wry.es.service.OmsOrderService;
import com.wry.es.utils.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <h1>  <h1>
 *
 * @author wry
 * @since 2024/9/14 14:20
 */
@Service
@Slf4j
public class HandleOrderService {

    @Resource
    private ElasticOrderRepository elasticOrderRepository;
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Resource
    private OmsOrderService omsOrderService;
    @Resource
    private OmsOrderMapper omsOrderMapper;


    public void save(List<OmsOrder> omsOrderList) {
        for (OmsOrder omsOrder : omsOrderList) {
            omsOrder.setCreatedAt(LocalDateTime.now());
        }
        elasticOrderRepository.saveAll(omsOrderList);
    }

    public void delById(Long id) {
        elasticOrderRepository.deleteById(id);
    }

    public void update(OmsOrder omsOrder) {
        elasticOrderRepository.save(omsOrder);
    }

    public Paging<OmsOrder> pageList(OrderPageDTO pageDTO) {
        PageInfo<OmsOrder> pageInfo = new PageInfo<>(pageDTO);
        IPage<OmsOrder> page = omsOrderMapper.pageAble(pageInfo, pageDTO);
        return PageUtils.toPage(page);
    }
}
